<template>
  <div :class="className" :style="{height:height,width:width}"/>
</template>

<script>
import echarts from 'echarts'
import resize from './mixins/resize'
import { statisticContainer } from '@/api/statistics/statistics'

require('echarts/theme/royal') // echarts theme

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '300px'
    }
  },
  data() {
    return {
      chart: null,
      legendData: [],
      seriesData: []
    }
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  created() {
    this.statisticContainer()
  },
  methods: {
    statisticContainer() {
      statisticContainer().then(response => {
        this.seriesData = response.data
        this.initChart()
      })
    },
    initChart() {
      this.chart = echarts.init(this.$el, 'royal')

      this.chart.setOption({
        title: {
          text: '容器统计',
          left: 'left'
        },
        tooltip: {
          trigger: 'item'
        },
        legend: {
          orient: 'vertical',
          left: 'right'
        },
        series: [
          {
            name: '当前存储数量',
            type: 'pie',
            radius: '50%',
            data: this.seriesData,
            emphasis: {
              itemStyle: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: 'rgba(0, 0, 0, 0.5)'
              }
            }
          }
        ]
      })
    }
  }
}
</script>
